<?php
class muser extends Model {
    var $table = "authniter_users";
    var $groups = "authniter_groups_users";
    var $id = "id";
    function add($data) {
        $data['password'] = md5($data['password']);
        $gu['group_id'] = $data['group_id'];
        unset($data['group_id']);
        $this->db->insert($this->table, $data);
        $id = $this->db->insert_id();
        $gu['user_id'] = $id;
        $this->db->insert($this->groups, $gu);
        return $id;
    }

    function edit($data) {
        $id = $data[ $this->id];
        $this->db->where($this->id, $id);
        $this->db->update($this->table, $data);
    }

    function delete($id) {
        $this->db->delete($this->table, array($this->id => $id));
    }

    function getAll() {
        $query = "select *, u.id as id from $this->table u, authniter_groups_users gu, authniter_groups g
                where gu.user_id=u.id and gu.group_id=g.id and g.id>1";
        return $this->db->query($query)->result_array();
    }
    function search($filter) {
        $query = "select * from $this->table where login like '%$filter%'";
        return $this->db->query($query)->result_array();
    }

    function get($id=0) {
        return $query = $this->db->get_where($this->table, array($this->id => $id))->row_array();
    }
    function get_by_login($login=0) {
        return $query = $this->db->get_where($this->table, array('login' => $login))->row_array();
    }

   function set_password($uid=0, $pwd=''){
       $this->db->where($this->id, $uid);
       $this->db->update($this->table, array('password'=> md5($pwd)));
       return $uid;
   }
}